public class DetectLinkedNodeCycle {
      class ListNode {
          int val;
          ListNode next;

          ListNode(int x) {
              val = x;
              next = null;
          }
      }
    public ListNode detectCycle(ListNode head) {
          ListNode fast = head;
          ListNode slow = head;
          while (fast != null && fast.next != null) {
              fast = fast.next.next;
              slow = slow.next;
              if (fast == slow) {
                  break;
              }
          }

          if (fast == null || fast.next == null) {
              return null;
          }

          slow = head;
          while (slow != fast) {
              slow = slow.next;
              fast = fast.next;
          }
          return slow;
    }
}
